package util

import (
	"strconv"
	"strings"
	"time"

	"mall/common/enumx"
	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/service/backend/common/utils/pointy"
)

const (
	arrayValueSeparator    = ","
	keyValueSeparator      = ":"
	keyOpSeparator         = ","
	arrayKeyValueSeparator = ";"
)

// ##25.03.24## 历史遗留文件，暂不处理
func GetFilterMap(filter string) map[string]string {
	filters := strings.Split(filter, arrayKeyValueSeparator)
	if len(filters) > 0 {
		res := make(map[string]string)
		for _, filterBy := range filters {
			filterBys := strings.Split(filterBy, keyValueSeparator)
			if len(filterBys) >= 2 && len(filterBys[0]) > 0 && len(filterBys[1]) > 0 {
				res[filterBys[0]] = filterBys[1]
			} else if len(filterBys) == 1 && len(filterBys[0]) > 0 {
				res[filterBys[0]] = "1"
			}
		}

		return res
	}

	return nil
}

func GetStatusText(status int32) (res string) { // 2023.12.23  todo  *uint32
	switch status {
	case globalkey.StatusApproved: // 审核通过
		res = "aproved"
	case globalkey.StatusPending: // 待审核
		res = "pending"
	case globalkey.StatusRejected: // 审核未通过
		res = "rejected"
	case globalkey.StatusDeleted: // 用户已删除
		res = "deleted"
	case globalkey.StatusCheckSuccess: // 待审核
		res = "pending"
	case globalkey.StatusDraft: // 草稿
		res = "draft"
	}

	return
}

func GetStatusTextNew(status *uint32) (res string) { // ##25.04.02##
	if status == nil {
		return
	}

	switch int32(*status) {
	case globalkey.StatusApproved:
		res = "审核通过"
	case globalkey.StatusPending:
		res = "待审核"
	case globalkey.StatusRejected:
		res = "审核驳回"
	case globalkey.StatusDeleted:
		res = "已删除"
	case globalkey.StatusCheckSuccess:
		res = "待审核"
	case globalkey.StatusDraft:
		res = "草稿"
	}

	return
}

func GetNoticeTypeText(orderType int32) (res string) { //////
	switch orderType {
	case 1:
		res = "香评审核通过"
	case 2:
		res = "香评审核驳回"
	case 3:
		res = "香评置顶"
	case 4:
		res = "香评取消置顶"
	}

	return
}

func GetNoticeStatusText(status int32) (res string) {
	switch enumx.ObjectStatus(status) {
	case enumx.ObjectStatusAproved: // 1
		res = "未读"
	case enumx.ObjectStatusRejected: // 2
		res = "已读"
	case enumx.ObjectStatusDeleted: // 3
		res = "用户已删除"
	}

	return
}

func GetCanPayText(timeString string) (res string) {
	localTime, err := modelx.ParseLocalTime(timeString)
	if err != nil {
		return res
	}

	now := time.Now() // 取到当前的时间点
	duration := localTime.Add(time.Duration(globalkey.CloseOrderTimeoutMinutes) * time.Minute).Sub(now)

	minutes := int(duration.Minutes()) // 分钟数
	if minutes > 0 {
		minutes = minutes % 60
		res += strconv.Itoa(minutes) + "分"
	}

	seconds := int(duration.Seconds()) // 秒数
	if seconds > 0 {
		seconds = seconds % 60
		if seconds < 10 {
			res += "0"
		}
		res += strconv.Itoa(seconds) + "秒"
	}

	return res
}

func GetTimeFromNow(timeString string) string {
	now := time.Now() // 取到当前的时间点
	localTime, err := modelx.ParseLocalTime(timeString)
	if err != nil {
		return timeString
	}

	duration := now.Sub(localTime)
	hours := int(duration.Hours()) // 已用小时数
	days := hours / 24             // 已用天数
	if days > 30 {
		return localTime.Format("2006-01-02")
	}

	if days > 0 {
		return strconv.Itoa(days) + "天前"
	}

	hours = hours % 24
	if hours > 0 {
		return strconv.Itoa(hours) + "小时前"
	}

	minutes := int(duration.Minutes()) // 已用分钟数
	if minutes > 0 {
		minutes = minutes % 60
		return strconv.Itoa(minutes) + "分钟前"
	}

	return "刚刚"
}

func GetOrderTypeText(orderType *uint32) (res *string) { // 2024.01.20
	if orderType != nil {
		switch *orderType {
		case globalkey.OrderTypeVirtual:
			res = pointy.GetPointer("宝石订单")
		case globalkey.OrderTypeCommon:
			res = pointy.GetPointer("实物订单")
		}
	}

	return
}

func GetProductTypeText(productType *uint32) (res *string) { // 2024.08.13
	if productType != nil {
		switch *productType {
		case globalkey.ProductTypePhysical:
			res = pointy.GetPointer("实物商品")
		case globalkey.ProductTypeVirtual:
			res = pointy.GetPointer("虚拟物品")
		case globalkey.ProductTypeCommon:
			res = pointy.GetPointer("原装正品")
		case globalkey.ProductTypeSample:
			res = pointy.GetPointer("试香小样")
		case globalkey.ProductTypeUnused:
			res = pointy.GetPointer("闲置")
		}
	}

	return
}

func GetTradeStateText(tradeState *uint32) (res *string) { // 2024.01.20
	if tradeState != nil {
		switch *tradeState {
		case globalkey.OrderTradeStateUserCancelled:
			res = pointy.GetPointer("已关闭") // 用户取消
		case globalkey.OrderTradeStateAdminCancelled:
			res = pointy.GetPointer("已关闭") // 后台取消
		case globalkey.OrderTradeStateAutoCancelled:
			res = pointy.GetPointer("已关闭") // 自动取消
		case globalkey.OrderTradeStateNotPay: //
			res = pointy.GetPointer("待付款")
		case globalkey.OrderTradeStateAdminRefunded:
			res = pointy.GetPointer("已退款")
		case globalkey.OrderTradeStateAdminRefunding:
			res = pointy.GetPointer("退款中")
		case globalkey.OrderTradeStateRefunded:
			res = pointy.GetPointer("已退款")
		case globalkey.OrderTradeStateRefunding:
			res = pointy.GetPointer("退款中")
		case globalkey.OrderTradeStateRefundApply:
			res = pointy.GetPointer("已申请退款")
		case globalkey.OrderTradeStatePaid: //
			res = pointy.GetPointer("待发货")
		case globalkey.OrderTradeStateReturned:
			res = pointy.GetPointer("已退货")
		case globalkey.OrderTradeStateReturning:
			res = pointy.GetPointer("退货中")
		case globalkey.OrderTradeStateReturnApply:
			res = pointy.GetPointer("已申请退货")
			// 售后申请中、申请拒绝、申请通过、等待退货、等待退款、售后完成
		case globalkey.OrderTradeStateShipping:
			res = pointy.GetPointer("备货中")
		case globalkey.OrderTradeStateShipped:
			res = pointy.GetPointer("已发货")
		case globalkey.OrderTradeStateDelivering:
			res = pointy.GetPointer("配送中")
		case globalkey.OrderTradeStateDelivered:
			res = pointy.GetPointer("已配送")
		case globalkey.OrderTradeStateConfirmed:
			res = pointy.GetPointer("交易完成")
		case globalkey.OrderTradeStateAutoConfirmed:
			res = pointy.GetPointer("交易完成")
		case globalkey.OrderTradeStateReviewed:
			res = pointy.GetPointer("已评价")
		case globalkey.OrderTradeStateAutoReviewed:
			res = pointy.GetPointer("自动评价")
		case globalkey.OrderTradeStateFulfilled:
			res = pointy.GetPointer("交易完成")
		case globalkey.OrderTradeStateClosed: // 用户已支付，并申请退款，取消订单并全额退款中
			res = pointy.GetPointer("退款中")
		}
	}

	return
}

type Option struct {
	CanCancel  bool
	CanPay     bool
	CanRefund  bool
	CanRemind  bool
	CanTrack   bool
	CanConfirm bool
	CanInvoice bool
	CanRemove  bool
}

func GetOptionByStatus(tradeState *uint32) *Option {
	res := &Option{}

	if tradeState != nil {
		switch *tradeState {
		case globalkey.OrderTradeStateNotPay:
			res.CanCancel = true
			res.CanPay = true
		case globalkey.OrderTradeStatePaid:
			res.CanRefund = true
			res.CanRemind = true
		case globalkey.OrderTradeStateRefunded:
			res.CanRemove = true
		case globalkey.OrderTradeStateShipping:
			res.CanRefund = true
		case globalkey.OrderTradeStateShipped:
			res.CanTrack = true
			res.CanConfirm = true
		case globalkey.OrderTradeStateFulfilled:
			res.CanInvoice = true
		case globalkey.OrderTradeStateUserCancelled, globalkey.OrderTradeStateAutoCancelled, globalkey.OrderTradeStateAdminCancelled:
			res.CanRemove = true
		case globalkey.OrderTradeStateConfirmed, globalkey.OrderTradeStateAutoConfirmed:
			res.CanInvoice = true
		}
	}

	return res
}

func GetReactionType(reactionType uint32) (res string) { // ##25.01.02##
	switch reactionType {
	case globalkey.ForumReactionTypeWant: // 想要
		res = "want"
	case globalkey.ForumReactionTypeOwn: // 拥有
		res = "own"
	case globalkey.ForumReactionTypeCollect: // 收藏
		res = "collect"
	}

	return
}

func GetForumReactionType(reactionType uint32) (res string) { // ##25.01.03##
	switch reactionType {
	case globalkey.ForumReactionTypeLike:
		res = "column:like"
	case globalkey.ForumReactionTypeCollect:
		res = "column:collect"
	case globalkey.ForumReactionTypeQuestionLike:
		res = "forum:question:like"
	}

	return
}
